/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */

package cn.webrx;

/**
 * <p>Project: jse2303 - Ex7</p>
 * <p>Powered by webrx On 2023-07-27 16:51:20</p>
 * <p>描述：<p>
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex7 {
    int num = 0;

    //CountDownLatch latch = new CountDownLatch(4);

    public static void main(String[] args) {
        long ss = System.currentTimeMillis();
        var ex = new Ex7();
        //var cc = new Thread(ex::show, "over");
        //cc.start();


        var t1 = new Thread(ex::m, "t1");
        var t2 = new Thread(ex::m, "t2");
        var t3 = new Thread(ex::m, "t3");
        var t4 = new Thread(ex::m, "t4");
        t1.start();
        t2.start();
        t3.start();
        t4.start();

        try {
            Thread.sleep(500);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            t1.join();
            t2.join();
            t3.join();
            t4.join();

        } catch (Exception e) {
            e.printStackTrace();
        }

        //try {
        //    ex.latch.await();
        //} catch (Exception e) {
        //    e.printStackTrace();
        //}

        System.out.println(ex.num);
        long ee = System.currentTimeMillis();
        System.out.println(ee - ss);
    }

    void m() {
        String t = Thread.currentThread().getName();
        System.out.printf("%s线程启动%n", t);
        for (int i = 0; i < 10000; i++) {
            synchronized (this) {
                ++num;
            }
        }
        System.out.printf("%s线程结束%n", t);
        //latch.countDown();
    }

    void show() {
        try {
            //latch.await();//
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(num);

    }
}
